package com.qidian.mes.cal.service.Impl;

import com.qidian.mes.cal.mapper.TeamMemberMapper;
import com.qidian.mes.cal.service.TeamMemberService;
import com.qidian.mes.cal.vo.TeamAndUserVO;
import com.qidian.mes.pojo.User;
import com.qidian.mes.util.pojo.MESResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class TeamMemberServiceImpl implements TeamMemberService {
    @Autowired
    TeamMemberMapper teamMemberMapper;

    @Override
    //根据用户名查询班组成员信息
    public MESResult selectByUserName(String userName) {
        return MESResult.ok(teamMemberMapper.selectByUserName(userName));
    }

    @Override
    //删除班组成员
    public MESResult deleteByIds(Long[] memberIds) {
        return MESResult.ok(teamMemberMapper.deleteByIds(memberIds));
    }

    @Override
    //获取所有成员
    public MESResult getAll() {
        return MESResult.ok(teamMemberMapper.getAll());
    }

    @Override
    //根据用户id查询数据
    public MESResult getByUserId(Long userId) {
        return MESResult.ok(teamMemberMapper.getByUserId(userId));
    }

    @Override
    //添加成员进班组
    public MESResult save(TeamAndUserVO vo) {
        List<User> list=vo.getUsers();
        for (int i = 0; i < list.size(); i++) {
            if (teamMemberMapper.getByUserId(list.get(i).getUserId()).equals(1)){
                list.remove(list.get(i));
                i--;
            }
        }
        if (list.size() == 0){
            return MESResult.ok();
        }
        return MESResult.ok(teamMemberMapper.save(vo.getTeamCode(),list));
    }

    @Override
    //根据班组编号查询班组成员
    public MESResult list(String teamCode) {
        return MESResult.ok(teamMemberMapper.list(teamCode));
    }

}
